package com.dongxing.concyrrency.chapter5;

/**
 * @author XavierDong
 **/
public class ThreadJoin3 {
    public static void main(String[] args) {

        Long startTime = System.currentTimeMillis();

        Thread t1 = new Thread(new CaptureRunnable("M1", 10000L));
        Thread t2 = new Thread(new CaptureRunnable("M2", 30000L));
        Thread t3 = new Thread(new CaptureRunnable("M3", 15000L));
        t1.start();
        t2.start();
        t3.start();

        try {
            // 主线程main会一直等待子线程结束，才会执行主线程main的方式
            t1.join();
            t2.join();
            t3.join();
        } catch (InterruptedException e) {
            e.printStackTrace();
        }

        Long endTime = System.currentTimeMillis();

        System.out.printf("Save data begin timestamp is:%s, end timestamp is:%s\n", startTime, endTime);


    }
}

class CaptureRunnable implements Runnable {

    private String machineName;

    private Long spendTime;

    public CaptureRunnable(String machineName, Long spendTime) {
        this.machineName = machineName;
        this.spendTime = spendTime;
    }

    @Override
    public void run() {
        // do the really capture data.
        try {
            Thread.sleep(spendTime);
            System.out.printf(machineName + " completed data capture at timestamp [%s] and successfully.\n", System.currentTimeMillis());
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }

    public String getResult() {
        return machineName + "finish.";
    }
}
